package com.jpa.gen;


import com.google.common.collect.Lists;
import org.jooq.codegen.GenerationTool;
import org.jooq.meta.jaxb.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Autor: songfei
 * @Description:
 * @Date: Created in 18:16 2024/8/30
 **/
@Component
public class Gen {
    private final List<String> tableNames = Lists.newArrayList("euclid_.*");
    public static final String DRIVER = "org.postgresql.Driver";
    @Value("${pg.url}")
    private String url;
    @Value("${pg.username}")
    private String userName;
    @Value("${pg.password}")
    private String password;

    public void genCode() {
        Configuration configuration = new Configuration()
                .withJdbc(
                        new Jdbc()
                                .withDriver(DRIVER)
                                .withUrl(url)
                                .withUser(userName)
                                .withPassword(password)
                )
                .withGenerator(
                        new Generator()
                                .withGenerate(
                                        new Generate()
                                                .withDaos(true)
                                                .withPojos(true)
                                                .withJavaTimeTypes(true)
                                                .withSpringAnnotations(true)
                                )
                                .withTarget(
                                        new Target()
                                                .withPackageName("com.jpa.jooq")
                                                .withDirectory("common-dao/src/main/java")
                                )
                                .withDatabase(
                                        new Database()
                                                .withName("org.jooq.meta.postgres.PostgresDatabase")
                                                .withInputSchema("public")
                                                .withIncludes(String.join("|", tableNames))

                                )
                );
        try {
            GenerationTool.generate(configuration);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
